package com.example.firstapp;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import util.Database;
import util.MyDatabaseHelper;

public class Test05_1Activity extends AppCompatActivity {
    private ListView lv_phoneNumber;
    private EditText et_find;
    private SQLiteDatabase db;
    List<Map<String, Object>> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test05_1);

        //链接数据库
        MyDatabaseHelper helper = new MyDatabaseHelper(this, "FistApp.db",
                null, 1);

        //获取数据库对象
        db = helper.getWritableDatabase();

        //绑定组件
        ImageButton imgBt_add = findViewById(R.id.imgBt_add);
        et_find = findViewById(R.id.et_find);
        lv_phoneNumber = findViewById(R.id.lv_phoneNumber1);

        //刷新通讯录列表
        updateList(db, null, null);

        //监听添加按钮
        imgBt_add.setOnClickListener(view -> {
            Intent intent = new Intent(this, Test05_2Activity.class);
            intent.putExtra("type", "0");
            startActivity(intent);
            Test05_1Activity.this.finish();
        });

        //查找框监听
        et_find.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                updateList(db, "name LIKE ? or phoneNumber LIKE ?", new String[]{
                        "%" + et_find.getText() + "%", "%" + et_find.getText() + "%"});
            }
        });

        //通讯录点击监听
        lv_phoneNumber.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String phone = String.valueOf(list.get(i).get("phoneNumber"));
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        ////通讯录长按监听
        lv_phoneNumber.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                Map<String, Object> user = list.get(i);
                String name = String.valueOf(user.get("name"));
                String phone = String.valueOf(user.get("phoneNumber"));
                Intent intent = new Intent(Test05_1Activity.this, Test05_2Activity.class);
                intent.putExtra("type", "1");
                intent.putExtra("name", name);
                intent.putExtra("phone", phone);
                startActivity(intent);
                Test05_1Activity.this.finish();
                return true;
            }
        });

    }

    public void updateList(SQLiteDatabase db, String selection, String[] selectionArgs) {
        list = Database.select_allPhone(db, "phoneNumber", selection, selectionArgs, "name");
        SimpleAdapter adapter = new SimpleAdapter(Test05_1Activity.this,
                list,
                R.layout.list_item,
                new String[]{"ico", "name", "phoneNumber"},
                new int[]{R.id.lv_img, R.id.tv_lv_name, R.id.tv_lv_text});
        lv_phoneNumber.setAdapter(adapter);
    }
}